package com.github.butid.data.netty.handler;

import io.netty.buffer.ByteBuf;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import lombok.extern.slf4j.Slf4j;

import java.nio.ByteOrder;
import java.nio.charset.Charset;

/**
 * 报文长度截取
 * @author wangjing.dc@qq.com
 * @since 2020/8/4
 */
@Slf4j
public class StringValueLengthFrameDecoder extends LengthFieldBasedFrameDecoder {


    public StringValueLengthFrameDecoder(int maxFrameLength,
                                         int lengthFieldOffset, int lengthFieldLength,
                                         int lengthAdjustment, int initialBytesToStrip) {

        super(maxFrameLength,
                lengthFieldOffset, lengthFieldLength, lengthAdjustment,
                initialBytesToStrip, true);

    }

    @Override
    protected long getUnadjustedFrameLength(ByteBuf buf, int offset, int length, ByteOrder order) {
        CharSequence len = buf.getCharSequence(offset, length, Charset.forName("utf-8"));
        log.info("报文长度={}", len);
        return Long.valueOf(String.valueOf(len));
    }

}
